package cn.edu.neusoft.yuejian12.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.edu.neusoft.yuejian12.Activity.ActivityInfoActivity;
import cn.edu.neusoft.yuejian12.Activity.MasterInfoActivity;
import cn.edu.neusoft.yuejian12.JavaBean.Master;

import cn.edu.neusoft.yuejian12.R;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 木子李 on 2017/5/19.
 */

public class HmasterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List mDataList;
    private LayoutInflater mLayoutInflater;
    public HmasterAdapter(Context mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }




    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.h_master_card, parent, false);
        return new HmasterAdapter.ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Master entity = (Master) mDataList.get(position);
        if (null == entity)
            return;
        HmasterAdapter.ViewHolder viewHolder = (HmasterAdapter.ViewHolder) holder;

        viewHolder.master_name.setText(entity.getRealname());

        viewHolder.master_hits.setText(entity.getHits());
        String photo = entity.getThumb();
        if (photo != ""){
            Picasso.with(mContext)
                    .load("http://172.24.10.175/workout/Uploads/"+entity.getThumb())
                    .into(viewHolder.master_pic);

            viewHolder.master_pic.setScaleType(ImageView.ScaleType.FIT_XY);

        }

        if(viewHolder.master_pic.getDrawable()==null){
            viewHolder.master_pic.setImageResource(R.drawable.no_pic);
            viewHolder.master_pic.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,MasterInfoActivity.class);
                intent.putExtra("master_id",entity.getId());
                intent.putExtra("master_name",entity.getRealname());
                intent.putExtra("master_pic", entity.getThumb());
                intent.putExtra("master_info",entity.getIntroduction());
                mContext.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView master_pic;
        TextView master_name,master_hits;




        public ViewHolder(View itemView) {
            super(itemView);
            master_hits = (TextView) itemView.findViewById(R.id.hits);
            master_name = (TextView) itemView.findViewById(R.id.master_name);
            master_pic = (ImageView) itemView.findViewById(R.id.master_pic);


        }
    }
}
